#include "basecore.hpp"

QImage g_showImage;

/***********************************************
 * 图片尺寸转换(根据label大小转换image大小
 ***********************************************/
QImage ScaleImage2Label(QImage qImage, QLabel* qLabel)
{
    QImage qScaledImage;
    QSize qImageSize = qImage.size();
    QSize qLabelSize = qLabel->size();
    double dWidthRatio = 1.0*qImageSize.width() / qLabelSize.width();
    double dHeightRatio = 1.0*qImageSize.height() / qLabelSize.height();
    if (dWidthRatio > dHeightRatio)
    {
        qScaledImage = qImage.scaledToWidth(qLabelSize.width());
    }
    else
    {
        qScaledImage = qImage.scaledToHeight(qLabelSize.height());
    }
    return qScaledImage;
}
QImage ScaleImage2Label(QImage qImage, QLabel* qLabel, int &imageshowx, int &imageshowy, int &imageshowwidth, int &imageshowheight)
{
    QImage qScaledImage;
    QSize qImageSize = qImage.size();
    QSize qLabelSize = qLabel->size();
    double dWidthRatio = 1.0*(qImageSize.width()) / (qLabelSize.width() - 2);
    double dHeightRatio = 1.0*(qImageSize.height()) / (qLabelSize.height() - 2);
    if (dWidthRatio > dHeightRatio)
    {
        qScaledImage = qImage.scaledToWidth(qLabelSize.width() - 2);
        imageshowx = 0;
        imageshowy = (qLabelSize.height() - 2 - qScaledImage.height()) / 2;
        imageshowwidth = qScaledImage.width();
        imageshowheight = qScaledImage.height();
    }
    else
    {
        qScaledImage = qImage.scaledToHeight(qLabelSize.height() - 2);
        imageshowx = (qLabelSize.width() - 2 - qScaledImage.width()) / 2;
        imageshowy = 0;
        imageshowwidth = qScaledImage.width();
        imageshowheight = qScaledImage.height();
    }
    return qScaledImage;
}

QImage ScaleImage(QImage qImage, QSize size)
{
    return qImage.scaled(size);
}

/***********************************************
 * 由文件完整路径得到文件所在目录路径
 ***********************************************/
QString PathxtoPath(QString pathx)
{
    QString name = pathx, path0;
    do
    {
        path0 = name;
        name = path0.section('/', 1);
    }
    while(!name.isEmpty());
    QString pat = pathx.remove("/" + path0);
    return pat;
}

